/**
 * osd 服务接口
 * @author niko
 * @deprecated 不再使用, 使用x-site-core-db中的osd服务
 */

import { osdClient } from '@wanmi/x-site-core-osd/lib'
let co = require('co');
import { commonUtil, constCommon } from '@qianmi/x-site-common';
let osdClientInstance;




/**
 * osd服务对象，只提供put Object操作
 * @date   2015-09-11
 * @author niko
 * @param  {String}   accessKeyId     osd Id
 * @param  {String}   accessKeySecret osd 密钥
 * @param  {String}   keyPath         保存在osd的路径,e.g. /qianmicom/d2c/dev/:adminID/[pc|mobile]/
 */
export class Client {
  osd: any;

  constructor(osdConfig) {
    this.osd = osdClient(osdConfig);
  }

  /**
   * 保存文件至osd
   * @date   2015-09-11
   * @author niko
   * @param  {String|Buffer|ReadStream}   src      filepath, new Buffer(), ReadStream
   * @param  {String}   adminID  商户id
   * @param  {String}   distName 存放的文件名字
   * @param  {Boolean}  isPC     是否PC端资源
   */
  putObject(src, distName): Promise<any> {
    return co(this.osd.putObject(src, distName));
  }

  /**
   *
   * @param src
   * @param distName
   * @returns {*}
   */
  putObjectAbsolute(src, absoluteDistName): Promise<any> {
    return co(
      this.osd.putObjectAbsolute(src, absoluteDistName)
    );
  }

  /**
   * 返回值 是一个buffer 对象..
   * @param distName
   * @returns {*}
   */
  getObject(distName): Promise<any> {
    return co(this.osd.getObject(distName));
  }

  getObjectAbsolute(absoluteDistName): Promise<any> {
    return co(this.osd.getObjectAbsolute(absoluteDistName));
  }

  /**
   * 删除数据
   * @param distName
   * @returns {*}
   */
  delObject(distName): Promise<any> {
    return co(this.osd.delObject(distName));
  }

  /**
   * Head an object and get the meta info.
   *
   *
   * @param distName
   * @returns {Promise<any>}
   */
  head(distName): Promise<any> {
    return co(this.osd.head(distName));
  }

  listObject(distDir): Promise<any> {
    return co(this.osd.listObject(distDir));
  }

  /**
   * 复制内容;;
   * @param name
   * @param sourceName
   * @param {{}} options
   * @returns {Promise<any>}
   */
  copy(name, sourceName, options = {}): Promise<any> {
    return co(this.osd.copy(name, sourceName, options));
  }

  /**
   *
   * @param options :{prefix:absoluteDistDir,"max-keys":1000}
   *                或 "x-site/prod/d2cStore/A1078241/view/";
   * @return {*}
   */
  listObjectAbsolute(
    absoluteDistDir,
  ): Promise<{
    res: any;
    objects: Array<any>;
    prefixes: string;
  }> {
    return co(this.osd.listObjectAbsolute(absoluteDistDir));
  }

  /**
   * 批量删除
   * @param distNames
   * @returns {*}
   */
  delMultiObjectAbsolute(distNames, options?): Promise<any> {
    return co(this.osd.delMultiObjectAbsolute(distNames, options));
  }
}

export function getOsdClient(osdConfig): Client {
  if (osdClientInstance) {
    return osdClientInstance;
  } else if (osdConfig && osdConfig.accessKeyId && osdConfig.accessKeySecret) {
    return (osdClientInstance = new Client(osdConfig));
  } else {
    throw new Error('必须传入OSD配置文件');
  }
}

export const getPrefix = (osdConfig): string => {
  if (osdConfig && osdConfig.bucketName && osdConfig.endPoint) {
    const endPoint = getEndPoint(osdConfig);
    const prefix = `https://${endPoint}`;
    console.log(prefix);
    return prefix;
  }else {
    throw new Error('必须传入OSD配置文件');
  }
}

export const getEndPoint = (osdConfig): string => {
  if (osdConfig && osdConfig.bucketName && osdConfig.endPoint) {
    return `${osdConfig.bucketName}.${osdConfig.endPoint}`;
  } else {
    throw new Error('必须传入OSD配置文件');
  }
}
